package com.tangcco.lockwell.utility;

import cn.hutool.core.collection.CollectionUtil;
import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.DataDictionary;
import com.tangcco.lockwell.domain.User;
import com.tangcco.lockwell.service.DataDictionaryService;
import com.tangcco.lockwell.service.UserService;
import com.tangcco.lockwell.viewmodel.admin.dataDictionary.DataDictionaryVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class StudentSubjectUtil {
    @Autowired
    private UserService userService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private WebContext webContext;
    @Value("${common.subjectMath}")
    private String subjectMath;
    @Value("${common.subjectEnglish}")
    private String subjectEnglish;
    @Value("${common.subjectPolitics}")
    private String subjectPolitics;

    public Integer getSubjectIdByUser(String subjectName){
        User u = userService.getUserById(webContext.getCurrentUser().getUserId());
        if(u!=null && u.getOptSubject()!=null){
            String[] subjects = u.getOptSubject().split(",");
            for (String s : subjects) {
                DataDictionary dataDictionary = dataDictionaryService.selectById(Integer.parseInt(s));
                if(dataDictionary.getDescription().contains(subjectName)){
                    return dataDictionary.getId();
                }
            }
        } else {
            //默认
            String categoryName = "";
            if(subjectName.contains("math")){
                categoryName = subjectMath;
            }else if(subjectName.contains("english")){
                categoryName = subjectEnglish;
            }else if(subjectName.contains("politics")){
                categoryName = subjectPolitics;
            }
            DataDictionaryVM dv = new DataDictionaryVM();
            dv.setCategoryName(categoryName);
            List<DataDictionaryVM> dataDictionaryVMS = dataDictionaryService.selectDataDictionaryByPage(dv);
            if(CollectionUtil.isNotEmpty(dataDictionaryVMS)){
                return dataDictionaryVMS.get(0).getId();
            }
        }
       return null;
    }

    public String getDefaultSubjectId(){
        try {
            List<Integer> subjects = new ArrayList<>();
            //默认
            DataDictionaryVM dv = new DataDictionaryVM();
            dv.setCategoryName(subjectEnglish);
            List<DataDictionaryVM> dataDictionaryVMS = dataDictionaryService.selectDataDictionaryByPage(dv);
            if(CollectionUtil.isNotEmpty(dataDictionaryVMS)){
                subjects.add(dataDictionaryVMS.get(0).getId());
            }
            DataDictionaryVM dv1 = new DataDictionaryVM();
            dv1.setCategoryName(subjectPolitics);
            List<DataDictionaryVM> dataDictionaryVMS1 = dataDictionaryService.selectDataDictionaryByPage(dv1);
            if(CollectionUtil.isNotEmpty(dataDictionaryVMS1)){
                subjects.add(dataDictionaryVMS1.get(0).getId());
            }
            DataDictionaryVM dv2 = new DataDictionaryVM();
            dv2.setCategoryName(subjectMath);
            List<DataDictionaryVM> dataDictionaryVMS2 = dataDictionaryService.selectDataDictionaryByPage(dv2);
            if(CollectionUtil.isNotEmpty(dataDictionaryVMS2)){
                subjects.add(dataDictionaryVMS2.get(0).getId());
            }
            return CollectionUtil.join(subjects, ",");
        } catch (Exception e) {
            return null;
        }
    }
}
